package com.crdeng.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crdeng.business.entity.Supplier;
import com.crdeng.business.service.SupplierService;
import com.crdeng.business.vo.SupplierQueryMap;
import com.crdeng.business.vo.SupplierVO;
import com.crdeng.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author crdeng
 * @since 2021-01-07
 */
@Api(value = "供应商接口",tags = "供应商接口")
@RestController
@RequestMapping("/business/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation(value = "供应商信息",notes = "获取供应商信息")
    @GetMapping("/getList")
    public Result getList(){

        List<Supplier> list = supplierService.list();

        return Result.ok().data("list",list);
    }

    @ApiOperation(value = "供应商列表",notes = "条件分页查询供应商列表")
    @PostMapping("/findSupplier")
    public Result findSupplier(@RequestParam(defaultValue = "1") Integer pageNun,
                               @RequestParam(defaultValue = "6") Integer pageSize,
                               @RequestBody SupplierQueryMap queryMap){
    IPage<Supplier> page=supplierService.findSupplier(pageNun,pageSize,queryMap);

        return Result.ok().data("list",page.getRecords()).data("total",page.getTotal());
    }


    @ApiOperation(value = "删除供应商信息",notes = "根据ID删除供应商信息")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long id){
         supplierService.deleteSupplierById(id);
        return Result.ok();
    }

    @ApiOperation(value = "编辑供应商信息",notes = "根据ID编辑供应商信息")
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody SupplierVO supplierVO){
     supplierService.editSupplierById(supplierVO);
        return Result.ok();
    }

    @ApiOperation(value = "添加供应商信息",notes = "添加供应商信息")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody SupplierVO supplierVO){
     supplierService.add(supplierVO);
        return Result.ok();
    }

}

